package cn.com.lsy.product.lepao.service.impl;

import cn.com.lsy.product.lepao.model.dao.LpBannerTblMapper;
import cn.com.lsy.product.lepao.model.dao.LpGoodsTblMapper;
import cn.com.lsy.product.lepao.model.dao.LpVersionTblMapper;
import cn.com.lsy.product.lepao.model.entities.*;
import cn.com.lsy.product.lepao.model.mt.dao.MtGoodsMapper;
import cn.com.lsy.product.lepao.service.ISystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SystemService implements ISystemService {

    @Autowired
    LpBannerTblMapper lpBannerTblMapper;
    @Autowired
    LpVersionTblMapper lpVersionTblMapper;
    @Autowired
    MtGoodsMapper mtGoodsMapper;
    @Autowired
    LpGoodsTblMapper lpGoodsTblMapper;

    @Override
    public List<LpBannerTbl> selectBannerList() {

        LpBannerTblExample example = new LpBannerTblExample();

        return lpBannerTblMapper.selectByExample(example);
    }

    @Override
    public LpBannerTbl selectBannerInfo(int id) {

        return lpBannerTblMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean updateBanner(LpBannerTbl param) {

        if (lpBannerTblMapper.updateByPrimaryKeySelective(param) > 0) {

            return true;

        } else {

            return false;
        }
    }

    @Override
    public boolean insertBanner(LpBannerTbl param) {

        if (lpBannerTblMapper.insertSelective(param) > 0) {

            return true;

        } else {

            return false;
        }
    }

    @Override
    public boolean delBanner(int id) {

        if (lpBannerTblMapper.deleteByPrimaryKey(id) > 0) {

            return true;

        } else {

            return false;
        }
    }

    @Override
    public List<LpVersionTbl> selectVersionList() {

        LpVersionTblExample example = new LpVersionTblExample();

        return lpVersionTblMapper.selectByExample(example);
    }

    @Override
    public boolean insertVersion(LpVersionTbl param) {

        //查询版本号是否存在
        LpVersionTblExample example = new LpVersionTblExample();
        example.createCriteria().andCodeEqualTo(param.getCode()).andTypeEqualTo(param.getType());

        if (lpVersionTblMapper.countByExample(example) > 0) {

            return false;
        }

        if (lpVersionTblMapper.insertSelective(param) > 0) {

            return true;

        } else {

            return false;
        }

    }

    @Override
    public boolean delVersion(int id) {

        if (lpVersionTblMapper.deleteByPrimaryKey(id) > 0) {

            return true;

        } else {

            return false;
        }
    }

    @Override
    public List<LpGoodsTbl> selectGoodsByPage(int start, int end, String type, String searchCondition) {

        Map<String, Object> where = new HashMap<String, Object>();
        where.put("start", start);
        where.put("end", end);
        where.put("type", type);
        where.put("searchCondition", searchCondition);

        return mtGoodsMapper.selectByPage(where);
    }

    @Override
    public int selectGoodsByCount(String type, String searchCondition) {

        Map<String, Object> where = new HashMap<String, Object>();
        where.put("type", type);
        where.put("searchCondition", searchCondition);

        return mtGoodsMapper.selectByCount(where);
    }

    @Override
    public LpGoodsTblWithBLOBs selectGoodsInfo(int id) {

        return lpGoodsTblMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean updateGoods(LpGoodsTblWithBLOBs param) {

        if (lpGoodsTblMapper.updateByPrimaryKeySelective(param) > 0) {

            return true;

        } else {

            return false;
        }
    }

    @Override
    public boolean insertGoods(LpGoodsTblWithBLOBs param) {

        if (lpGoodsTblMapper.insertSelective(param) > 0) {

            return true;

        } else {

            return false;
        }
    }

    @Override
    public boolean delGoods(int id) {

        LpGoodsTblWithBLOBs lpGoodsTbl = new LpGoodsTblWithBLOBs();
        lpGoodsTbl.setId(id);
        lpGoodsTbl.setIsDelete("1");

        if (lpGoodsTblMapper.updateByPrimaryKeySelective(lpGoodsTbl) > 0) {

            return true;

        } else {

            return false;
        }
    }
}
